package net.messagenet.mazor.infra.persistence;

import java.util.LinkedList;
import java.util.List;

import net.messagenet.mazor.DO.CustomerDO;
import net.messagenet.mazor.DO.DpaDO;
import net.messagenet.mazor.DO.DpaProductDO;
import net.messagenet.mazor.DO.PosDO;
import net.messagenet.mazor.DO.PosProductDO;
import net.messagenet.mazor.DO.ProductDO;
import net.messagenet.mazor.infra.persistence.entities.Customer;
import net.messagenet.mazor.infra.persistence.entities.Dpa;
import net.messagenet.mazor.infra.persistence.entities.DpaProduct;
import net.messagenet.mazor.infra.persistence.entities.Pos;
import net.messagenet.mazor.infra.persistence.entities.PosProduct;
import net.messagenet.mazor.infra.persistence.entities.Product;

/**
 * This class is used to transform all the data object to entities bean
 * and vies versa 
 * 
 * @author Idan
 *
 */

public class DataObjectConverter {

	public Dpa convert(DpaDO dpa) {
		if (dpa==null)
			return null;
		Dpa dpaEntity= new Dpa();
		dpaEntity.setId(dpa.getId());
		dpaEntity.setRequestDate(dpa.getRequestDate());
		dpaEntity.setAdditionalInformation(dpa.getAdditionalInformation());
		dpaEntity.setContact(dpa.getContact());
		dpaEntity.setDialogicfse(dpa.getDialogicfse());
		dpaEntity.setEmail(dpa.getEmail());
		dpaEntity.setCc(dpa.getCc());
		dpaEntity.setEndUserName(dpa.getEndUserName());
		dpaEntity.setExpirationDate(dpa.getExpirationDate());
		dpaEntity.setDpaId(dpaEntity.getDpaId());
		dpaEntity.setReasonForRequest(dpa.getReasonForRequest());
		dpaEntity.setFileName(dpa.getFileName());
		dpaEntity.setDraft(dpa.isDraft());
		dpaEntity.setApproved(dpa.isApproved());
		dpaEntity.setClosed(dpa.isClosed());
		//dpaEntity.setCustomer(convert(dpa.getCustomer()));
		return dpaEntity;
	}

	/**
	 * converts a customer data object to a Customer entity
	 * 
	 * @param dpa
	 * @return
	 */
	public Customer convert(CustomerDO customer) {
		if (customer==null)
			return null;
		Customer ce = new Customer();
		ce.setId(customer.getId());
		ce.setName(customer.getName());
		ce.setLocation(customer.getLocation());
		ce.setAccountancyId(customer.getAccountancyId());
		ce.setState(customer.getState());
		ce.setCountry(customer.getCountry());
		ce.setZipCode(customer.getZipCode());
		return ce;
	}

	public CustomerDO convert(Customer c) {
		if (c==null)
			return null;
		CustomerDO customerDO=new CustomerDO();
		customerDO.setId(c.getId());
		customerDO.setName(c.getName());
		customerDO.setLocation(c.getLocation());
		customerDO.setAccountancyId(c.getAccountancyId());
		customerDO.setState(c.getState());
		customerDO.setCountry(c.getCountry());
		customerDO.setZipCode(c.getZipCode());
		
		return customerDO;
	}

	public DpaProduct convert(DpaProductDO dpaProductDO) {
		if (dpaProductDO==null)
			return null;
		Product p = new Product();
		p.setDialogicItemKey(dpaProductDO.getProduct().getDialogicItemKey());
		p.setId(dpaProductDO.getProduct().getId());
		p.setProductName(dpaProductDO.getProduct().getProductName());
		
		DpaProduct dpaProduct = new DpaProduct();
		dpaProduct.setApprovedPrice(dpaProductDO.getApprovedPrice());
		dpaProduct.setCompetitor(dpaProductDO.getCompetitor());
		dpaProduct.setCompetitorsPrice(dpaProductDO.getCompetitorsPrice());
		dpaProduct.setId(dpaProductDO.getId());
		
		
		dpaProduct.setQuantity(dpaProductDO.getQuantity());
		dpaProduct.setSdc(dpaProductDO.getQuantity());
		dpaProduct.setTargetCost(dpaProductDO.getTargetCost());
		dpaProduct.setProduct(p);
		dpaProduct.setQuantityReported(dpaProductDO.getQuantityReported());
		dpaProduct.setQuantityPaid(dpaProductDO.getQuantityPaid());
		return dpaProduct;
	}

	public ProductDO convert(Product c) {
		ProductDO prDo = new ProductDO();
		prDo.setDialogicItemKey(c.getDialogicItemKey());
		prDo.setId(c.getId());
		prDo.setProductName(c.getProductName());
		return prDo;
	}

	public DpaDO convert(Dpa dpa) {
		if (dpa==null)
			return null;
		DpaDO dpaDo= new DpaDO();
		dpaDo.setId(dpa.getId());
		dpaDo.setRequestDate(dpa.getRequestDate());
		dpaDo.setAdditionalInformation(dpa.getAdditionalInformation());
		dpaDo.setContact(dpa.getContact());
		dpaDo.setDialogicfse(dpa.getDialogicfse());
		dpaDo.setEmail(dpa.getEmail());
		dpaDo.setCc(dpa.getCc());
		dpaDo.setEndUserName(dpa.getEndUserName());
		dpaDo.setExpirationDate(dpa.getExpirationDate());
		dpaDo.setFileName(dpa.getFileName());
		dpaDo.setDpaId(dpa.getDpaId());
		dpaDo.setDraft(dpa.isDraft());
		dpaDo.setApproved(dpa.isApproved());
		dpaDo.setClosed(dpa.isClosed());
		dpaDo.setReasonForRequest(dpa.getReasonForRequest());
		return dpaDo;
	}

	public DpaProductDO convert(DpaProduct product) {
		if (product==null)
			return null;
		DpaProductDO dpaProductDO = new DpaProductDO();
		dpaProductDO.setId(product.getId());
		dpaProductDO.setApprovedPrice(product.getApprovedPrice());
		dpaProductDO.setCompetitor(product.getCompetitor());
		dpaProductDO.setCompetitorsPrice(product.getCompetitorsPrice());
		dpaProductDO.setProduct(convert(product.getProduct()));
		dpaProductDO.setQuantity(product.getQuantity());
		dpaProductDO.setQuantityReported(product.getQuantityReported());
		dpaProductDO.setQuantityPaid(product.getQuantityPaid());
		dpaProductDO.setSdc(product.getSdc());
		dpaProductDO.setTargetCost(product.getTargetCost());
		return dpaProductDO;
	}

	public Pos convert(PosDO posDo) {
		if (posDo==null)
			return null;
		Pos posRep = new Pos();
		posRep.setEmail(posDo.getEmail());
		posRep.setCc(posDo.getCc());
		posRep.setDate(posDo.getDate());
		posRep.setId(posDo.getId());
		posRep.setDraft(posDo.isDraft());
		posRep.setFileName(posDo.getFileName());
		return posRep;
	}

	public PosProduct convert(PosProductDO poProductDO) {
		if (poProductDO==null)
			return null;
		PosProduct posProduct = new PosProduct();
		posProduct.setCurrency(poProductDO.getCurrency());
		posProduct.setDpaCost(poProductDO.getDpaCost());
		posProduct.setDpaId(poProductDO.getDpaId());
		posProduct.setId(poProductDO.getId());
		posProduct.setInvoiceLine(poProductDO.getInvoiceLine());
		posProduct.setInvoiceNumber(poProductDO.getInvoiceNumber());
		posProduct.setQuantity(poProductDO.getQuantity());
		posProduct.setResalePrice(poProductDO.getResalePrice());
		posProduct.setSdc(poProductDO.getSdc());
		posProduct.setShipDate(poProductDO.getShipDate());
		//posProduct.setDpaProduct(convert(poProductDO.getDpaProduct()));
		return posProduct;
	}
	public PosProductDO convert(PosProduct posPrd) {
		if (posPrd==null)
			return null;
		PosProductDO posDo = new PosProductDO();
		posDo.setCurrency(posPrd.getCurrency());
		posDo.setDpaCost(posPrd.getDpaCost());
		posDo.setDpaId(posPrd.getDpaId());
		posDo.setId(posPrd.getId());
		posDo.setInvoiceLine(posPrd.getInvoiceLine());
		posDo.setInvoiceNumber(posPrd.getInvoiceNumber());
		posDo.setQuantity(posPrd.getQuantity());
		posDo.setResalePrice(posPrd.getResalePrice());
		posDo.setSdc(posPrd.getSdc());
		posDo.setShipDate(posPrd.getShipDate());
		return posDo;
	}
	public PosDO fullConvert(Pos pos) {
		if (pos==null)
			return null;
		PosDO posDO = new PosDO();
		posDO.setEmail(pos.getEmail());
		posDO.setCc(pos.getCc());
		posDO.setDate(pos.getDate());
		posDO.setId(pos.getId());
		posDO.setDraft(pos.isDraft());
		posDO.setFileName(pos.getFileName());
		//copying the product list
		List<PosProductDO> list = new LinkedList<PosProductDO>();
		for (PosProduct posPrd : pos.getPosProducts()) {
			PosProductDO prdDo = convert(posPrd);
			prdDo.setCustomer(convert(posPrd.getCustomer()));
			prdDo.setPos(posDO);
			prdDo.setProduct(convert(posPrd.getProduct()));
			prdDo.setDpaProduct(convert(posPrd.getDpaProduct()));
			list.add(prdDo);
		}
		 
		posDO.setPosProducts(list);
		return posDO;
	}



	public DpaDO fullConvert(Dpa dpa) {
		DpaDO dpaDO = convert(dpa);
		dpaDO.setCustomer(convert(dpa.getCustomer()));
		
		//checking if the the products from the dpa has more units to use
		List<DpaProductDO> dpasprodDO = new LinkedList<DpaProductDO>();
		for (DpaProduct product: dpa.getDpaProducts()) {
			DpaProductDO prdDo =convert(product);
			prdDo.setDpa(dpaDO);
			dpasprodDO.add(prdDo);
		}
		//the dpa is not empty and has product to use
		dpaDO.setDpaProducts(dpasprodDO);
		
		return dpaDO;
	}
	public void fillDpaProductData(PosProduct posPrd, PosProductDO poProductDO) {
		posPrd.setCurrency(poProductDO.getCurrency());
		posPrd.setDpaCost(poProductDO.getDpaCost());
		posPrd.setDpaId(poProductDO.getDpaId());
		posPrd.setId(posPrd.getId());
		posPrd.setInvoiceLine(poProductDO.getInvoiceLine());
		posPrd.setInvoiceNumber(poProductDO.getInvoiceNumber());
		posPrd.setQuantity(poProductDO.getQuantity());
		posPrd.setResalePrice(poProductDO.getResalePrice());
		posPrd.setSdc(poProductDO.getSdc());
		posPrd.setShipDate(poProductDO.getShipDate());
		posPrd.setDpaProduct(convert(poProductDO.getDpaProduct()));
		
	}
	public void fillDpaProductData(DpaProduct dpaProduct,
			DpaProductDO dpaProductDO) {
		
		dpaProduct.setApprovedPrice(dpaProductDO.getApprovedPrice());
		dpaProduct.setCompetitor(dpaProductDO.getCompetitor());
		dpaProduct.setQuantity(dpaProductDO.getQuantity());
		dpaProduct.setSdc(dpaProductDO.getSdc());
		dpaProduct.setTargetCost(dpaProductDO.getTargetCost());
		
	}

	public void fillPOSData(Pos posReport, PosDO pos) {
		posReport.setEmail(pos.getEmail());
		posReport.setCc(pos.getCc());
		posReport.setDate(pos.getDate());
		
	}

	public void fillDpaData(Dpa dpaEnt, DpaDO dpa) {
		dpaEnt.setAdditionalInformation(dpa.getAdditionalInformation());
		dpaEnt.setCc(dpa.getCc());
		dpaEnt.setContact(dpa.getContact());
		dpaEnt.setDialogicfse(dpa.getDialogicfse());
		dpaEnt.setEmail(dpa.getEmail());
		dpaEnt.setEndUserName(dpa.getEndUserName());
		dpaEnt.setFileName(dpa.getFileName());
		dpaEnt.setReasonForRequest(dpa.getReasonForRequest());
		dpaEnt.setRequestDate(dpa.getRequestDate());
		
	}
}
